<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.thymeleaf.org"
      layout:decorator="layout/master">
<body layout:fragment="content">
<div th:replace="layout/layout::center_zy_search_top_box('拼多多商品转链')"></div>
<!-- 内容框 -->
<div class="zy_module-content">
    <div class="vip_user_msg">
        <form>
            <div class="three">
                <div class="zy_title_con">
                    <h2><span>需要转换的拼多多链接<i></i></span></h2>
                </div>
                <textarea id="sourceUrl"></textarea>
            </div>
            <div class="three">
                <div class="zy_title_con">
                    <h2><span>转换完成<i></i></span></h2>
                </div>
                <!--                <div class="yht_cp_btn btn" id="divTargetUrl" cp-id="targetUrl"></div>-->
                <textarea id="targetUrl" readonly="readonly"></textarea>
            </div>
        </form>
    </div>
    <div class="vip_user_addr_new vip_user_addr_dizhi">
        <div class="box dizhi_new">
            <form>
                <div class="dizhi_foot dis_flex">
                    <h2>
                        <input type="button" value="一键复制" onclick="copy()">
                        <input type="button" value="开始转换" onclick="convert()">
                    </h2>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="/js/layer/layer.js"></script>
<script src="/js/clipboard.js"></script>
<script>
    function convert() {
        var sourceUrl = $("#sourceUrl").val();
        if (sourceUrl == "") {
            layer.msg("需要转换的链接不能是空的");
            return;
        }
        $.post("/ajax/product/convert", {sourceUrl: sourceUrl}, function (d) {
            if (d.code == 0) {
                layer.msg("转换成功");
                $("#divTargetUrl").val(d.data);
                $("#targetUrl").val(d.data);
            } else {
                layer.msg(d.msg);
            }
        })
    }

    function copy() {
        var targetUrl = $("#targetUrl").val();
        if (targetUrl == "") {
            layer.msg('复制错误，未找到数据');
        }
        try {
            navigator.clipboard.writeText($("#targetUrl").val()).then(function () {
                layer.msg('复制成功');
            }, function () {
                layer.msg("复制失败,请手动选择复制");
            });
        } catch (e) {
            try{
                $("#targetUrl").select(); // 选择对象
                document.execCommand("Copy"); // 执行浏览器复制命令
                layer.msg('复制成功');
            }catch(e1){
                layer.msg("复制失败");
            }
        }
    }
</script>

</body>
</html>